package br.com.ordersys.web.client.modules.uicomp;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;

public class ClickableHPanel extends HorizontalPanel {
	private boolean pressed;
	private PressHandler hnd;
	private String unpressedStyle;
	private String pressedStyle;

	public ClickableHPanel(String style1,String style2) {
		super();
		setStyleName(style1);
		this.unpressedStyle = style1;
		this.pressedStyle = style2;
		
		addDomHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent e) {
				if (!pressed) {
					pressed = true;
					setStyleName(pressedStyle);
					hnd.onPress(e);
				} else {
					unpress();
					hnd.onUnPress(e);
				}
				e.stopPropagation();
			}
		}, ClickEvent.getType());

	}

	public void setPressHandler(PressHandler hnd) {
		this.hnd = hnd;
	}

	public void unpress() {
		setStyleName(unpressedStyle);
		pressed = false;
	}

	public static interface PressHandler {
		void onPress(ClickEvent e);

		void onUnPress(ClickEvent e);
	}
}
